package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.Category;
import com.zrrd.reggie.bean.Employee;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/page")
    public R<Page> findAllCategory(int page,int pageSize){
        QueryWrapper wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        Page<Category> pageInfo =new Page<>(page,pageSize);
        categoryService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }
    //添加分类
    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        boolean save = categoryService.save(category);
        return save?R.success("添加成功"):R.error("添加失败");
    }
    //修改分类
    @PutMapping
    public R<String> update(@RequestBody Category category){
        return categoryService.updateById(category)==true?R.success("修改成功"):R.error("修改失败");
    }
    //删除分类
    @DeleteMapping
    public R<String> delete(Long id){
        return categoryService.removeById(id)==true?R.success("删除成功"):R.error("删除失败");
    }
    //根据类型查询分类 type=1:菜品 。。。
    @GetMapping("/list")
    public R<List<Category>> list(Integer type){

        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        if (type!=null){
            wrapper.eq("type",type);
        }

        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }
}
